var content = document.querySelector('.content');
var tbody1 = document.querySelector(".grade-table tbody");
var tbody2 = document.querySelector(".grade-table-fk tbody");
var tui1 = document.querySelector('.tui');
var tui2 = document.querySelector('.tui2');
var targetContainer = document.querySelector('.grade-table-fk');
var tmp = 0;

content.addEventListener('mouseenter', function() {
  content.style.transition = 'background-color 0.3s';
  content.style.backgroundColor = 'rgba(100,100,100,0.5)'; // 将透明度更改为 0.5，使背景色加深
});

content.addEventListener('mouseleave', function() {
  content.style.transition = 'background-color 0.3s';
  content.style.backgroundColor = 'rgba(100,100,100,0.2)'; // 将透明度恢复为 0.2，使背景色变浅
});

var data1 = [];
var data2 = [];
//test

document.addEventListener("DOMContentLoaded", function() {

  $.ajax({
    url: '/management/stuservlet/stuCourseNotSigned',                                 //url方法接口：获取需要签到的课程
    method: 'POST',
    data: {
      username: "1"
    },
    success: function(response) {
      console.log(response);
      console.log(response.data);
      data1 = response.data;
      console.log(data1.length);
      if (data1.length == 0) {
        tui1.style.display = 'block';
      } else {
        tui1.style.display = 'none';
        // 将数据数组赋值给相应的元素
        var currentIndex = 1;
        data1.forEach(function(item) {
          var row = document.createElement("tr");
          row.innerHTML = `
            <td>${currentIndex}</td>
            <td>${item.Cou_id}</td>
            <td>${item.Cou_name}</td>
            <td>${item.Cou_time}</td>
            <td>${item.Cou_teacher}</td>
            <td><button id="jjw${currentIndex}">去签到</button></td>
          `;
          tbody1.appendChild(row);
          currentIndex++;
        });
        for (let i = 1; i < currentIndex; i++) {
          var jjw = document.getElementById('jjw' + i);
          jjw.addEventListener('click', function() {
              $.ajax({
                url: '/management/stuservlet/stuCourseSign',                                 //url方法接口：签到
                method: 'POST',
                data: {
                  count: i
                },
                success: function(response) {
                  console.log(response);
                  console.log(response.data);
                  console.log(i);
                  alert("签到成功！");
                  window.location.href = "stulogin_live.html"
                }
              });
          });
        }
      }
    }
  });

  $.ajax({
    url: '/management/stuservlet/stuAdvice',                                 //url方法接口：获取需要反馈的课程
    method: 'POST',
    data: {
      username: "1"
    },
    success: function(response) {
      console.log(response);
      console.log(response.data);
      data2 = response.data;

      //test
      //data2 = test_data2;

      //console.log(data2.length);
      if (data2.length == 0) {
        tui2.style.display = 'block';
      } else {
        tui2.style.display = 'none';
        var currentIndex = 1;
        data2.forEach(function(item) {
          var row = document.createElement("tr");
          row.innerHTML = `
            <td>${currentIndex}</td>
            <td>${item.Cou_id}</td>
            <td>${item.Cou_name}</td>
            <td>${item.Cou_time}</td>
            <td>${item.Cou_teacher}</td>
            <td><button id="jjw2${currentIndex}">去反馈</button></td>
          `;
          tbody2.appendChild(row);
          currentIndex++;
        });
        for (let i = 1; i < currentIndex; i++) {
          var jjw = document.getElementById('jjw2' + i);
            jjw.addEventListener('click', function() {

              if (tmp == 0) {
                tmp = 1;
                // 创建表单元素
                var form = document.createElement("form");
                form.id = "feedbackForm";

                // 创建输入框
                var input = document.createElement("input");
                input.type = "text";
                input.name = "feedbackInput";
                form.appendChild(input);

                // 创建取消按钮
                var cancelBtn = document.createElement("button");
                cancelBtn.type = "button";
                cancelBtn.innerText = "取消";
                form.appendChild(cancelBtn);

                // 创建提交按钮
                var submitBtn = document.createElement("button");
                submitBtn.type = "submit";
                submitBtn.innerText = "提交";
                form.appendChild(submitBtn);

                $(document).keydown(function (event) {
                  if (event.keyCode == 13) { // 13代表回车键的键码
                    $('#feedbackForm button[type="submit"]').click();
                  }
                });
                // 将表单添加到页面中
                //document.body.appendChild(form);

                // 将表单插入到目标容器的下方
                targetContainer.parentNode.insertBefore(form, targetContainer.nextSibling);

                // 提交表单的事件处理程序
                form.addEventListener('submit', function (event) {
                  event.preventDefault(); // 阻止表单默认提交行为
                  var confirmSubmit = confirm("确认提交吗？");

                  // 根据用户的选择执行相应的操作
                  if (confirmSubmit) {
                    // 获取用户输入的反馈信息
                    var feedback = input.value;
                    console.log(i);
                    console.log(feedback);

                    // 发送AJAX请求提交反馈信息
                    $.ajax({
                      url: '/management/stuservlet/SaveApplication',                                 //url方法接口：反馈
                      method: 'POST',
                      data: {
                        count: i,
                        feedback: feedback
                      },
                      success: function (response) {
                        console.log(response);
                        console.log(response.flag);
                        alert("反馈成功！");
                        tmp = 0;
                        // 移除表单元素
                        form.remove();
                        window.location.href = "stulogin_live.html"
                      }
                    });
                  }
                  form.submit();
                });
              }
              // 取消按钮的事件处理程序
              cancelBtn.addEventListener('click', function () {
                // 移除表单元素
                form.remove();
                tmp = 0;
              });

            });
            }

        }
      }

  });
});

  var links = document.querySelectorAll('.sidebar ul li a');

  for (var i = 0; i < links.length; i++) {
    links[i].addEventListener('mouseenter', function() {
      this.style.transition = '0.2s';
      this.style.backgroundColor = '#679';
      this.style.borderRadius = '10px'; // 设置边缘的圆角半径为10px
    });

    links[i].addEventListener('mouseleave', function() {
      this.style.transition = '0.2s';
      this.style.backgroundColor = '';
      this.style.borderRadius = '10px'; // 鼠标移出时恢复原始边缘
    });
  }

